<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 首页
 */
declare (strict_types=1);

namespace app\api\controller;

use app\api\base\controller\Base;
use app\extend\model\Banner;
use app\extend\model\ProductData;

class Home extends Base
{
    /**
     * 轮播图
     */
    public function getBannerAll()
    {
        $list = Banner::where([
            ['status','=',1]
        ])->order(['sort' => 'asc','id' => 'asc'])->select();

        return json(_S('获取成功',$list));
    }

    /**
     * 排名前三的商品
     */
    public function getHotProductAll()
    {
        $productList = \app\extend\model\Product::where([
            ['sale', '<>', 0],
            ['is_delete','=',0]
        ])->order(['sort' => 'asc','id' => 'asc'])->field('id,product_name,sale')->limit(3)->select();

        if(!$productList->isEmpty()){
            foreach($productList as $productVal){
                $productDataInfo = ProductData::where([
                    ['product_id', '=', $productVal->id]
                ])->field('id,price')->find();
                $productVal->price = $productDataInfo['price'];
                $productVal->chajia = round(($productDataInfo['price']-$productVal['sale']),2);
                if($productDataInfo['price'] > $productVal['sale']){
                    $productVal->class = 'red';
                }else{
                    $productVal->class = 'green';
                }
            }
        }

        return json(_S('获取成功',$productList));
    }
}
